﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Browser;
using System.Text;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.Prompt;


namespace Baosight.ZNH.ZMCB.UI
{
    public partial class ZMCB08 : EFViewCmes
    {
        public ZMCB08()
        {
            InitializeComponent();
            //base.BeforeAction += OnBeforeAction;
        }
        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //public void OnBeforeAction(object sender, EventArgs e)
        //{
        //    EFViewCmesProperty _view = new EFViewCmesProperty();
        //    _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
        //    base.ViewProperty = _view;
        //}

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);

            //初始条件的设置
            if ((initData.GetAttributes().ContainsKey("initParam")))
            {
                string initParam = initData.Get("initParam").ToString();
                string[] Params = initParam.Split(':');
                string fdKeyId = Params[1].ToString();
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCB08");
                eiInfo.Set("fdMainId", fdKeyId);
                eiInfo.Set("method", "query");
                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, QueryCompleted);
            }
        }

        private void QueryCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.EIDataContext.Update(e.EIInfo);
            if (e.EIInfo.GetStatus() != -1)
            {
                for (int i = 0; i < e.EIInfo.GetBlock("result").GetRowCount(); i++)
                {
                    if (e.EIInfo.GetBlock("result").GetRow(i)["fdPosition"].ToString().Equals("东"))
                    {
                        this.fdNeighborhoodIdEast.Value = e.EIInfo.GetBlock("result").GetRow(i)["fdNeighborhoodId"];
                    }
                    else if (e.EIInfo.GetBlock("result").GetRow(i)["fdPosition"].ToString().Equals("西"))
                    {
                        this.fdNeighborhoodIdWest.Value = e.EIInfo.GetBlock("result").GetRow(i)["fdNeighborhoodId"];
                    }
                    else if (e.EIInfo.GetBlock("result").GetRow(i)["fdPosition"].ToString().Equals("南"))
                    {
                        this.fdNeighborhoodIdNorth.Value = e.EIInfo.GetBlock("result").GetRow(i)["fdNeighborhoodId"];
                    }
                    else if (e.EIInfo.GetBlock("result").GetRow(i)["fdPosition"].ToString().Equals("北"))
                    {
                        this.fdNeighborhoodIdSouth.Value = e.EIInfo.GetBlock("result").GetRow(i)["fdNeighborhoodId"];
                    }
                    else if (e.EIInfo.GetBlock("result").GetRow(i)["fdPosition"].ToString().Equals("上"))
                    {
                        this.fdNeighborhoodIdFront.Value = e.EIInfo.GetBlock("result").GetRow(i)["fdNeighborhoodId"];
                    }
                    else
                    {
                        this.fdNeighborhoodIdUnder.Value = e.EIInfo.GetBlock("result").GetRow(i)["fdNeighborhoodId"];
                    }
                }
            }
            else
            {
                base.OpenEFWindowAlert(e.EIInfo.GetMsg(), true);
                base.TempButtonOpen(true);
            }
        }
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            EIInfo retData = new EIInfo();
            EIBlock adjoiningBlock = getBlock("VALUES");
            //EIBlock OppositeAdjoiningBlock = getBlock("OppositeAdjoiningBlock");
            string fdNeighborhoodId = "";
            string fdNeighborhood = "";
            if (this.fdNeighborhoodIdEast.Value != null && !this.fdNeighborhoodIdEast.Value.ToString().Equals(""))
            {
                fdNeighborhoodId += "东:" + this.fdNeighborhoodIdEast.Value + ";";
                fdNeighborhood += "东:" + ((this.fdNeighborhoodIdEast.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"] + ";";
                //EIRow rowEast = new EIRow();
                ////rowEast.Add("fdMainId", this.fdMainId.Value);
                //rowEast.Add("fdNeighborhoodId", "东:" + this.fdNeighborhoodIdEast.Value);
                //rowEast.Add("fdNeighborhood", "东:" + ((this.fdNeighborhoodIdEast.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"]);
                ////rowEast.Add("fdPosition", "东");
                //adjoiningBlock.AddRow(rowEast);

                //EIRow rowEastOpposite = new EIRow();
                //rowEastOpposite.Add("fdMainId", this.fdNeighborhoodIdEast.Value);
                //rowEastOpposite.Add("fdNeighborhoodId", this.fdMainId.Value);
                //rowEastOpposite.Add("fdPosition", "西");
                //OppositeAdjoiningBlock.AddRow(rowEastOpposite);
            }

            if (this.fdNeighborhoodIdWest.Value != null && !this.fdNeighborhoodIdWest.Value.ToString().Equals(""))
            {

                fdNeighborhoodId += "西:" + this.fdNeighborhoodIdWest.Value + ";";
                fdNeighborhood += "西:" + ((this.fdNeighborhoodIdWest.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"] + ";";
                //EIRow rowWest = new EIRow();
                ////rowWest.Add("fdMainId", this.fdMainId.Value);
                //rowWest.Add("fdNeighborhoodId", "西:" + this.fdNeighborhoodIdWest.Value);
                //rowWest.Add("fdNeighborhood", "西:" + ((this.fdNeighborhoodIdWest.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"]);
                ////rowWest.Add("fdPosition", "西");
                //adjoiningBlock.AddRow(rowWest);

                //EIRow rowWestOpposite = new EIRow();
                //rowWestOpposite.Add("fdMainId", this.fdNeighborhoodIdWest.Value);
                //rowWestOpposite.Add("fdNeighborhoodId", this.fdMainId.Value);
                //rowWestOpposite.Add("fdPosition", "东");
                //OppositeAdjoiningBlock.AddRow(rowWestOpposite);
            }

            if (this.fdNeighborhoodIdSouth.Value != null && !this.fdNeighborhoodIdSouth.Value.ToString().Equals(""))
            {
                fdNeighborhoodId += "南:" + this.fdNeighborhoodIdSouth.Value + ";";
                fdNeighborhood += "南:" + ((this.fdNeighborhoodIdSouth.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"] + ";";
                //EIRow rowSouth = new EIRow();
                ////rowSouth.Add("fdMainId", this.fdMainId.Value);
                //rowSouth.Add("fdNeighborhoodId", "南:" + this.fdNeighborhoodIdSouth.Value);
                //rowSouth.Add("fdNeighborhood", "南:" + ((this.fdNeighborhoodIdSouth.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"]);
                ////rowSouth.Add("fdPosition", "南");
                //adjoiningBlock.AddRow(rowSouth);

                //EIRow rowSouthOpposite = new EIRow();
                //rowSouthOpposite.Add("fdMainId", this.fdNeighborhoodIdSouth.Value);
                //rowSouthOpposite.Add("fdNeighborhoodId", this.fdMainId.Value);
                //rowSouthOpposite.Add("fdPosition", "北");
                //OppositeAdjoiningBlock.AddRow(rowSouthOpposite);
            }

            if (this.fdNeighborhoodIdNorth.Value != null && !this.fdNeighborhoodIdNorth.Value.ToString().Equals(""))
            {
                fdNeighborhoodId += "北:" + this.fdNeighborhoodIdNorth.Value + ";";
                fdNeighborhood += "北:" + ((this.fdNeighborhoodIdNorth.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"] + ";";
                //EIRow rowNorth = new EIRow();
                //rowNorth.Add("fdMainId", this.fdMainId.Value);
                //rowNorth.Add("fdNeighborhoodId", "北:" + this.fdNeighborhoodIdNorth.Value);
                //rowNorth.Add("fdNeighborhood", "北:" + ((this.fdNeighborhoodIdNorth.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"]);
                //rowNorth.Add("fdPosition", "北");
                //adjoiningBlock.AddRow(rowNorth);

                //EIRow rowNorthOpposite = new EIRow();
                //rowNorthOpposite.Add("fdMainId", this.fdNeighborhoodIdNorth.Value);
                //rowNorthOpposite.Add("fdNeighborhoodId", this.fdMainId.Value);
                //rowNorthOpposite.Add("fdPosition", "南");
                //OppositeAdjoiningBlock.AddRow(rowNorthOpposite);
            }

            if (this.fdNeighborhoodIdFront.Value != null && !this.fdNeighborhoodIdFront.Value.ToString().Equals(""))
            {
                fdNeighborhoodId += "上:" + this.fdNeighborhoodIdFront.Value + ";";
                fdNeighborhood += "上:" + ((this.fdNeighborhoodIdFront.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"] + ";";
               // EIRow rowFront = new EIRow();
               // //rowFront.Add("fdMainId", this.fdMainId.Value);
               // rowFront.Add("fdNeighborhoodId", "上:" + this.fdNeighborhoodIdFront.Value);
               // rowFront.Add("fdNeighborhood", "上:" + ((this.fdNeighborhoodIdFront.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"]);
               //// rowFront.Add("fdPosition", "上");
               // adjoiningBlock.AddRow(rowFront);

                //EIRow rowFrontOpposite = new EIRow();
                //rowFrontOpposite.Add("fdMainId", this.fdNeighborhoodIdFront.Value);
                //rowFrontOpposite.Add("fdNeighborhoodId", this.fdMainId.Value);
                //rowFrontOpposite.Add("fdPosition", "下");
                //OppositeAdjoiningBlock.AddRow(rowFrontOpposite);
            }

            if (this.fdNeighborhoodIdUnder.Value != null && !this.fdNeighborhoodIdUnder.Value.ToString().Equals(""))
            {
                fdNeighborhoodId += "下:" + this.fdNeighborhoodIdUnder.Value + ";";
                fdNeighborhood += "下:" + ((this.fdNeighborhoodIdUnder.Content as EFComboBox).SelectedItem as UIBaseEntity).GetEIRow()["fdName"] + ";";
               // EIRow rowUnder = new EIRow();
               //// rowUnder.Add("fdMainId", "下:"+ this.fdMainId.Value);
               // rowUnder.Add("fdNeighborhoodId", "下:" + this.fdNeighborhoodIdUnder.Value);
               // rowUnder.Add("fdNeighborhood", "下:" + ((this.fdNeighborhoodIdUnder.Content as EFComboBox).SelectedItem as UIBaseEntity ).GetEIRow()["fdName"]);
               //// rowUnder.Add("fdPosition", "下");
               // adjoiningBlock.AddRow(rowUnder);

                //EIRow rowUnderOpposite = new EIRow();
                //rowUnderOpposite.Add("fdMainId", this.fdNeighborhoodIdUnder.Value);
                //rowUnderOpposite.Add("fdNeighborhoodId", this.fdMainId.Value);
                //rowUnderOpposite.Add("fdPosition", "上");
                //OppositeAdjoiningBlock.AddRow(rowUnderOpposite);
            }
            EIRow row = new EIRow();
            row.Add("fdNeighborhoodId", fdNeighborhoodId);
            row.Add("fdNeighborhood", fdNeighborhood);
            adjoiningBlock.AddRow(row);
            retData.AddBlock(adjoiningBlock);
            //retData.AddBlock(OppositeAdjoiningBlock);
            NotifyViewDataChanged(retData);
        }

        private EIBlock getBlock(String BlockName)
        {
            EIBlock block = new EIBlock(BlockName);
            EIColumn fdKeyId = new EIColumn();
            fdKeyId.Name = "fdKeyId";
            EIColumn fdMainId = new EIColumn();
            fdMainId.Name = "fdMainId";
            EIColumn fdNeighborhoodId = new EIColumn();
            fdNeighborhoodId.Name = "fdNeighborhoodId";
            EIColumn fdNeighborhood = new EIColumn();
            fdNeighborhood.Name = "fdNeighborhood";
            EIColumn fdPosition = new EIColumn();
            fdPosition.Name = "fdPosition";
            block.AddColumn(fdKeyId);
            block.AddColumn(fdMainId);
            block.AddColumn(fdNeighborhoodId);
            block.AddColumn(fdNeighborhood);
            block.AddColumn(fdPosition);
            return block;
        }
    }
}